package com.zmn.plat.dubbo.impl.channel.workorder.video;

import com.alibaba.fastjson.JSON;
import com.aliyun.openservices.shade.com.google.common.collect.Lists;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.common.dictionary.ProductDict;
import com.zmn.plat.common.dto.channel.workorder.video.ChannelWorkOrderVideoDIO;
import com.zmn.plat.common.dto.channel.workorder.video.ChannelWorkOrderVideoDRO;
import com.zmn.plat.dubbo.interfaces.channel.workorder.video.ChannelWorkOrderVideoListRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.entity.channel.Channel;
import com.zmn.plat.model.entity.channel.workorder.video.ChannelWorkOrderVideo;
import com.zmn.plat.model.entity.channel.workorder.video.ChannelWorkOrderVideoQuery;
import com.zmn.plat.services.interfaces.channel.ChannelService;
import com.zmn.plat.services.interfaces.channel.workorder.video.ChannelWorkOrderVideoService;
import com.zmn.plat.utils.bean.BeanCopyUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.apache.logging.log4j.util.Strings;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 类描述：渠道-工单视频
 *
 * @author yangshuangping
 * @since 2021/06/09 14:00
 */
@Slf4j
@Service(version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class ChannelWorkOrderVideoListRemoteServiceImpl implements ChannelWorkOrderVideoListRemoteService {

    @Resource
    private ChannelWorkOrderVideoService channelWorkOrderVideoService;

    @Resource
    private ChannelService channelService;

    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<ChannelWorkOrderVideoDRO>> listByQuery(ChannelWorkOrderVideoDIO channelWorkOrderVideoDIO) {

        log.info("ChannelWorkOrderVideoListRemoteServiceImpl.listByQuery入参：{}", JSON.toJSONString(channelWorkOrderVideoDIO));

        ChannelWorkOrderVideoQuery query = new ChannelWorkOrderVideoQuery();
        BeanCopyUtils.copyProperties(channelWorkOrderVideoDIO, query);
        // 是否存在渠道
        Channel channel = channelService.findByKey(query.getChannelId());
        if (Objects.isNull(channel)) {
            return ResponseDTO.fail("渠道不存在");
        }
        // 判断该渠道下是否存在工单视频
        List<ChannelWorkOrderVideo> channelWorkOrderVideos = Optional.ofNullable(channelWorkOrderVideoService.listByQuery(query))
                .orElse(Collections.emptyList());
        if (CollectionUtils.isEmpty(channelWorkOrderVideos)) {
            return ResponseDTO.fail("没有相关渠道视频配置信息");
        }

        BeanCopier copier = BeanCopier.create(ChannelWorkOrderVideo.class, ChannelWorkOrderVideoDRO.class, false);
        List<ChannelWorkOrderVideoDRO> result = channelWorkOrderVideos.stream().map(video -> {
            ChannelWorkOrderVideoDRO dro = new ChannelWorkOrderVideoDRO();
            copier.copy(video, dro, null);
            dro.setBizName(bizTypeConvertBizName(video.getBizType()));
            return dro;
        }).collect(Collectors.toList());

        return ResponseDTO.success(result);
    }

    /**
     * 根据bizType转中文
     *
     * @param bizType 业务类型 多个以“,” 隔开
     * @return 业务类型中文
     */
    private String bizTypeConvertBizName(String bizType) {
        List<String> bizName = Lists.newArrayList();
        List<String> bizTypes = Lists.newArrayList(bizType.split(",", -1));
        for (String x : bizTypes) {
            try {
                bizName.add(ProductDict.getBizTypeCNENName(Integer.valueOf(x)));
            } catch (Exception e) {
                log.error("#channel-orderVideo-bizType-conversionErrors# error:{}", e.getMessage(), e);
            }
        }
        return Strings.join(bizName, ',');
    }
}
